// myself/file/ChooseUser.js
import api from "../../utils/api.js";
import utils from "../../utils/utils.js";
import network from "../../utils/network.js";
import formatDate from "../../utils/format_date.js";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    items: [],
    selectValue: [],
    selectName: [],
    select_all: false,
    isselect:false
  },

  checkboxChange(e) {
    const items = this.data.items
    const values = e.detail.value
    var arr = [];
    for (let i = 0, lenI = items.length; i < lenI; ++i) {
      items[i].checked = false
      for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
        if (items[i].id === values[j]) {
          items[i].checked = true
          arr.push(items[i].name)
          break
        }
      }
    }
    this.setData({
      items: items,
      selectValue: e.detail.value,
      selectName: arr
    })
    if(this.data.selectValue.length == 0){
      this.setData({
        isselect:false,
        select_all:false
      })
    }else{
      if(this.data.selectValue.length == items.length){
        this.setData({
          select_all:true
        })
      }else{
        if(this.data.selectValue.length < items.length){
          this.setData({
            select_all:false
          })
        }
      }
     
    }
   
    
  },
  selectAll(e) { //全选与反全选
    console.log(this.data.select_all)
    var arr = [];
    var seleValue = [];
    for (let i = 0; i < this.data.items.length; i++) {
      this.data.items[i].checked = (!this.data.select_all)
      if (!this.data.select_all) {
        arr.push(this.data.items[i].name)
        seleValue.push(this.data.items[i].id)
      }
    }

    this.setData({
      items: this.data.items,
      selectValue: seleValue,
      selectName: arr,
      select_all: (!this.data.select_all)
    })
    if(!this.data.select_all){
      this.setData({
        isselect:false
      })
    }

  },
  determine(e) {
    var selectValue = this.data.selectValue
    var selectName = this.data.selectName
    if(!this.data.isselect){
      if (selectValue.length <= 0) {
        this.toast.showToast('请至少选择一个');
        return;
      }
    }
    var ss = selectValue.join(',')
    var nn = selectName.join(',')
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面

    prevPage.setData({
      nextname: nn,
      nextnameid: ss
    });
    //返回上一页
    wx.navigateBack({
      delta: 1
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let nexuids = options.nexuids;
    let selectid = options.selectid;
    let nextname = options.nextname;
    let selearr = ''
    if (selectid) {
      selearr = selectid.split(',')
      this.setData({
        selectValue:selearr
      })
    }
    let selearrname = ''
    if(nextname){
      selearrname = nextname.split(',')
      this.setData({
        selectName:selearrname
      })
    }
    network.postJson(this, api.getNextUser, {
      adminid: app.globalData.userInfo.uid,
      token: app.globalData.userInfo.token,
      nexuids: nexuids,
    }).then(res => {
      let result = res.data.data;
      if (selearr) {
        var num = 0
        for (let i = 0; i < result.length; i++) {
          for (let j = 0; j < selearr.length; j++) {
            if (result[i].id == selearr[j]) {
              result[i].checked = true
              num++;
              break
            }
          }
        }
        if(num > 0){
          this.setData({
            isselect:true
          })
          if(num == result.length){
            this.setData({
              select_all: true
            })
          }
        }
      }
      this.setData({
        items: result
      })
    })
    this.toast = this.selectComponent("#toast");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})